<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<style>
			h5 {
			margin: 5px 7px;
		}
	</style>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">重置书籍</h1>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded" style="margin: 8%;">
				<div class="mui-input-row mui-search">
					<input type="search" class="mui-input-clear" placeholder="在此输入书籍名称" onkeydown="search();" id="key">
				</div>
			</div>
			<div id="text" style="display: none;">
				<div class="mui-card" style="margin: 8%;">
					<ul class="mui-table-view" id="dataList">
						<li class="mui-table-view-divider">搜索结果</li>
					</ul>
				</div>
			</div>
		</div>
	</body>
	<script src="../js/config.js"></script>
	<script src="../js/book.js"></script>
	<script src="../js/mui.min.js"></script>
	<script type="text/javascript" charset="utf-8">
		var key;
		mui.init();
		mui.plusReady(function() {
			document.body.querySelector("#key").focus();
		});

		function search() {
			if (event.keyCode == 13) {
				var key = document.getElementById("key").value;
				if (key == "") return;
				mui.ajax(config.config.path + "/book/search?key=" + key, {
					type: "GET",
					dataType: "json",
					success: function(result) {
						document.getElementById("key").blur();
						document.getElementById("text").style.display = 'block';
						var code = result.code;
						if (code == 204) {
							var ele = document.getElementById("dataList");
							var top = '<li class="mui-table-view-divider">搜索结果，共0条相关记录</li>';
							ele.innerHTML = top;
							mui.toast('未找到相关信息', {
								duration: 'short',
								type: 'div'
							})
						} else if (code == 400) {
							var ele = document.getElementById("dataList");
							var top = '<li class="mui-table-view-divider">搜索结果，共0条相关记录</li>';
							ele.innerHTML = top;
							mui.toast('未找到相关信息', {
								duration: 'short',
								type: 'div'
							})
						} else if (code == 200) {
							var ele = document.getElementById("dataList");
							var top = '<li class="mui-table-view-divider">搜索结果，共' + result.data.length + '条相关记录</li>';
							var data = "";
							for (j = 0, len = result.data.length; j < len; j++) {
								data = data + ' <li class="mui-table-view-cell">' 
								+'<div class="mui-slider-right mui-disabled">'
								+'<a class="mui-btn mui-btn-yellow mui-icon mui-icon-reload" id="' + result.data[j].id  + '-reload"></a>'
								+'<a class="mui-btn mui-btn-red mui-icon mui-icon-trash" id="' + result.data[j].number  + '-delete"></a></div>'
								+'<div class="mui-slider-handle"><div class="mui-table-cell">'
								+ result.data[j].name + '<span class="mui-badge">' + result.data[j].author + '</span>'
								+'</div></div></li>';
								data = data.replace(new RegExp(key, 'g'), '<font color="red">' + key + '</font>');
							}
							ele.innerHTML = top + data; 
						}
					}
				});
			}
		}

		
		mui("#dataList").on('tap', '.mui-btn', function() {
			var elem = this.parentNode.parentNode;
			//获取id
			var id = this.getAttribute("id").split("-")[0];
			var type = this.getAttribute("id").split("-")[1];
			if(type == 'reload'){
				mui.confirm('确认重置书籍吗？', '提示', ['确认', '取消'], function(e) {
					if (e.index == 0) {
						mui.swipeoutClose(elem);
						mui.ajax(config.config.path + "/book/reload/" + id, {
							type: "GET",
							dataType: "json",
							success: function(result) {
								var code = result.code;
								if (code == 200) {
									mui.toast('重置成功', {
										duration: 'short',
										type: 'div'
									});
								} else {
									mui.toast(result.message, {
										duration: 'short',
										type: 'div'
									});
								}
							}
						});
					}
				}, 'div');
			}else if(type == 'delete'){
				mui.confirm('确认删除书籍吗？', '提示', ['确认', '取消'], function(e) {
					if (e.index == 0) {
						mui.ajax(config.config.path + "/book/delete/" + id, {
							type: "GET",
							dataType: "json",
							success: function(result) {
								var code = result.code;
								if (code == 200) {
									mui.toast('删除成功', {
										duration: 'short',
										type: 'div'
									});
								} else {
									mui.toast(result.message, {
										duration: 'short',
										type: 'div'
									});
								}
								elem.style.display = "none";
							}
						});
					}
				}, 'div');
			}
		});
	</script>
</html>
